package com.wei.czz.framework.store.manager;

import com.wei.czz.common.dto.common.GroupValue;
import com.wei.czz.common.dto.common.NameValue;
import com.wei.czz.common.dto.store.account.AccountEnumDto;
import com.wei.czz.common.dto.store.audit.AuditStoreDto;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.enums.store.StoreSubmitTypeEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.vo.store.StoreAccountVo;
import com.wei.czz.framework.common.service.RegionCodeService;
import com.wei.czz.framework.store.entity.AuditStoreEntity;
import com.wei.czz.framework.store.entity.StoreAccountEntity;
import com.wei.czz.framework.store.service.*;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-12-27 09:56:26
 * className: StoreAccountManager
 * version: 1.0
 * description:
 */
@Component
@AllArgsConstructor
public class StoreAccountManager {

    private static final Logger log = LoggerFactory.getLogger(StoreAccountManager.class);

    private final StoreService storeService;

    private final StoreAccountService storeAccountService;

    private final StoreBossService storeBossService;

    private final AssignStoreService assignStoreService;

    private final AuditStoreService auditStoreService;

    private final RegionCodeService regionCodeService;

    @Transactional
    public void saveStoreAccount(StoreAccountVo storeAccountVo) {

        /*
            校验商店是否存在正在审核中的账户
         */
        StoreAccountEntity storeAccount = storeAccountService.getStoreAuditAccount(storeAccountVo.getStoreId());
        if (Objects.nonNull(storeAccount)) {
            log.info("商店存在正在审核中的账户，添加失败");
            throw new CzzException("您当前有正在审核中的商店账户，添加失败。");
        }

        /*
            获取商店账户
         */
        if (Objects.nonNull(storeAccountVo.getId())) {
            storeAccount = storeAccountService.getStoreAccount(storeAccountVo.getId());
            if (CommonEnum.ZERO.getValue().equals(storeAccount.getAuditStatus())) {
                log.info("商店账户正在审核中，不允许修改");
                throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "商店账户正在审核中，不允许修改");
            }
        }

        /*
            保存商店账户
         */
        storeAccountVo.setDefaultStatus(CommonEnum.ONE.getValue());
        storeAccountVo.setAuditStatus(CommonEnum.ZERO.getValue());
        storeAccountService.saveStoreAccount(storeAccountVo);

        /*
            添加商店账户审核分单
         */
        assignStoreService.saveAssignStore(storeAccountVo.getStoreId(),
                StoreSubmitTypeEnum.ADD_STORE_ACCOUNT.getValue());
    }

    public AccountEnumDto getStoreAccountEnums(Long id, Long storeId) {

        /*
            获取下一个排序等级
         */
        Integer sort = storeAccountService.getNextSort(storeId);

        /*
            获取开户城市枚举列表
         */
        List<GroupValue<NameValue<Integer>>> bankCityList = regionCodeService.getCityList();

        /*
            商店账户最新一条审核拒绝记录
         */
        AuditStoreDto auditStoreDto = null;
        if (Objects.nonNull(id)) {
            List<AuditStoreEntity> auditStoreList = auditStoreService.getAuditStoreList(null, id,
                    null);
            auditStoreDto = auditStoreList.stream()
                    .filter(auditStore -> CommonEnum.ONE.getValue().equals(auditStore.getStatus()))
                    .max(Comparator.comparing(AuditStoreEntity::getId))
                    .map(AuditStoreDto::map)
                    .orElse(null);
        }

        AccountEnumDto accountEnumDto = new AccountEnumDto();
        accountEnumDto.setSort(sort)
                .setBankCityList(bankCityList)
                .setAuditStoreDto(auditStoreDto);
        return accountEnumDto;
    }

    @Transactional
    public void updateStoreAccount(StoreAccountVo storeAccountVo) {

        /*
            获取商店账户
         */
        StoreAccountEntity storeAccount = storeAccountService.getStoreAccount(storeAccountVo.getId());
        if (CommonEnum.ZERO.getValue().equals(storeAccount.getDefaultStatus())) {
            log.info("商店默认账户不允许直接修改");
            throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "商店默认账户不允许直接修改");
        }

        /*
            校验商店是否存在正在审核中的账户
         */
        storeAccount = storeAccountService.getStoreAuditAccount(storeAccountVo.getStoreId());
        if (Objects.nonNull(storeAccount)) {
            throw new CzzException("您当前有正在审核中的商店账户，添加失败。");
        }

        /*
            保存商店账户
         */
        storeAccountVo.setDefaultStatus(CommonEnum.ONE.getValue());
        storeAccountVo.setAuditStatus(CommonEnum.ZERO.getValue());
        storeAccountService.saveStoreAccount(storeAccountVo);

        /*
            添加商店账户审核分单
         */
        assignStoreService.saveAssignStore(storeAccountVo.getStoreId(),
                StoreSubmitTypeEnum.STORE_ACCOUNT_MODIFY.getValue());
    }

    @Transactional
    public void deleteStoreAccount(Long id) {
        /*
            获取商店账户
         */
        StoreAccountEntity storeAccount = storeAccountService.getStoreAccount(id);
        if (CommonEnum.ZERO.getValue().equals(storeAccount.getDefaultStatus())) {
            log.info("商店默认账户不允许删除");
            throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "商店默认账户不允许删除");
        }

        /*
            删除商店账户
         */
        storeAccountService.deleteStoreAccount(id);

        /*
            删除商店账户审核分单
         */
        if (CommonEnum.ZERO.getValue().equals(storeAccount.getAuditStatus())) {
            List<Integer> submitTpeList = Arrays.asList(StoreSubmitTypeEnum.ADD_STORE_ACCOUNT.getValue(),
                    StoreSubmitTypeEnum.STORE_ACCOUNT_MODIFY.getValue());
            assignStoreService.batchDeleteAssignStore(Collections.singletonList(storeAccount.getStoreId()), submitTpeList);
        }
    }
}
